/**
 * 
 */
package by.imotaeva.printing_services.services.impl;

import java.util.List;

import javax.inject.Inject;

import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import by.imotaeva.printing_services.dataaccess.BlankOrderDao;
import by.imotaeva.printing_services.dataaccess.PrintingProductOrderDao;
import by.imotaeva.printing_services.datamodel.BlankOrder;
import by.imotaeva.printing_services.datamodel.PrintingProductOrder;
import by.imotaeva.printing_services.services.BlankOrderService;

/**
 * @author Inna
 *
 */
@Service
public class BlankOrderServiceImpl implements BlankOrderService {
	private static final Logger LOGGER = LoggerFactory
			.getLogger(BlankOrderServiceImpl.class);

	@Inject
	private BlankOrderDao dao;

	@Inject
	private PrintingProductOrderDao printingDao;

	@Override
	public void createNewBlankOrder(BlankOrder blankOrder,
			PrintingProductOrder printingProductOrder) {
		Validate.isTrue(
				blankOrder.getId() == null,
				"This method should be called for 'not saved yet' blankOrder only. Use UPDATE instead");
		printingDao.insert(printingProductOrder);
		blankOrder.setPrintingProductOrder(printingProductOrder);
		dao.insert(blankOrder);
	}

	@Override
	public void update(BlankOrder entity) {
		dao.update(entity);

	}

	@Override
	public BlankOrder get(Long id) {

		return dao.getById(id);

	}

	@Override
	public void removeBlankOrder(Long id) {
		dao.delete(id);
		printingDao.delete(id);
	}

	@Override
	public List<BlankOrder> getAllBlankOrders() {
		return dao.getAll();
	}

	@Override
	public void deleteAllBlankOrders() {
		LOGGER.debug("Remove all blank orders");

		dao.deleteAll();
		printingDao.deletePrintingProductOrder(getBlankOrderIds());

	}

	// private List<Long> getBlankOrdersId() {
	// List<BlankOrder> list = dao.getAll();
	// List<Long> listId = new ArrayList<Long>();
	// for (BlankOrder listElement : list) {
	// Long blankId = listElement.getId();
	// listId.add(blankId);
	// }
	// return listId;
	// }

	@Override
	public List<Long> getBlankOrderIds() {
		return dao.getBlankOrderIds();
	}

	@Override
	public Long getCountBlankOrders() {
		return dao.getCount();
	}

	@Override
	public BlankOrder getFullBlankOrder(Long id) {
		return dao.getFullBlankOrder(id);
	}

}
